package com.eova.widget;

/**
 * @author zhaopan
 * @date :2017/4/17
 * @Description:
 */

import com.alibaba.fastjson.JSONObject;
import com.eova.common.utils.data.CloneUtil;
import com.eova.common.utils.db.DbUtil;
import com.eova.common.utils.xx;
import com.eova.engine.DynamicParse;
import com.eova.engine.EovaExp;
import com.eova.model.MetaField;
import com.eova.model.MetaObject;
import com.eova.model.User;
import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

public class WidgetController extends Controller {
    public WidgetController() {
    }

    public void toFind() {
        this.render("/eova/widget/find/find.html");
    }

    public void find() {
        String url = "/widget/findJson?";
        String exp = this.getPara("exp");
        String code = this.getPara("code");
        String field = this.getPara("field");
        boolean isMultiple = this.getParaToBoolean("multiple", Boolean.valueOf(false)).booleanValue();
        if (xx.isEmpty(exp)) {
            MetaField se = MetaField.dao.getByObjectCodeAndEn(code, field);
            exp = se.getStr("exp");
            url = url + "code=" + code + "&field=" + field;
        } else {
            url = url + "exp=" + exp;

            try {
                exp = URLDecoder.decode(exp, "UTF-8");
            } catch (UnsupportedEncodingException var9) {
                var9.printStackTrace();
            }
        }

        exp = DynamicParse.buildSql(exp, new Object[]{this.getSessionAttr("user")});
        EovaExp se1 = new EovaExp(exp);
        MetaObject mo = se1.getObject();
        List mfs = se1.getFields();
        if (isMultiple) {
            mo.set("is_single", Boolean.valueOf(false));
        }

        this.setAttr("action", url);
        this.setAttr("objectJson", JsonKit.toJson(mo));
        this.setAttr("fieldsJson", JsonKit.toJson(mfs));
        this.setAttr("itemList", mfs);
        this.toFind();
    }

    public void findJson() {
        String exp = this.getPara("exp");
        String code = this.getPara("code");
        String en = this.getPara("field");
        String like=this.getPara("like");
        User user = User.dao.getByLoginId(getPara("userId"));
        this.setSessionAttr("user",user);
        if (xx.isEmpty(exp)) {
            MetaField se = MetaField.dao.getByObjectCodeAndEn(code, en);
            exp = se.getStr("exp");
        } else {
            try {
                exp = URLDecoder.decode(exp, "UTF-8");
            } catch (UnsupportedEncodingException var18) {
                var18.printStackTrace();
            }
        }

        exp = DynamicParse.buildSql(exp, new Object[]{this.getSessionAttr("user")});
        EovaExp se1 = new EovaExp(exp);
        String select = se1.simpleSelect;
        String from = se1.from;
        String where = se1.where;
        if(!xx.isEmpty(like)){
            where = where+" and cn like '%"+like+"%'";
        }
        String ds = se1.ds;
        List eis = se1.getFields();
        int pageNumber = this.getParaToInt("page", Integer.valueOf(1)).intValue();
        int pageSize = this.getParaToInt("rows", Integer.valueOf(100000)).intValue();
        ArrayList parmList = new ArrayList();
        where = WidgetManager.getWhere(this, eis, parmList, where);
        Object[] parm = new Object[parmList.size()];
        parmList.toArray(parm);
        String sort = WidgetManager.getSort(this, se1.order);
        String sql = from + where + sort;
        Page page = Db.use(ds).paginate(pageNumber, pageSize, select, DbUtil.formatSql(sql), parm);
        String json = JsonKit.toJson(page.getList());
        json = "{\"total\":" + page.getTotalRow() + ",\"rows\":" + json + "}";
        this.removeSessionAttr("user");
        this.renderJson(json);
    }

    public void findCnByEn() {
        String code = this.getPara(0);
        String en = this.getPara(1);
        String value = this.getPara(2);
        MetaField ei = MetaField.dao.getByObjectCodeAndEn(code, en);
        String exp = ei.getStr("exp");
        exp = DynamicParse.buildSql(exp, new Object[]{this.getSessionAttr("user")});
        EovaExp se = new EovaExp(exp);
        String ds = se.ds;
        StringBuilder sb = new StringBuilder();
        String sql;
        if (!xx.isEmpty(value)) {
            sb.append(se.pk);
            sb.append(" in(");
            String[] var12;
            int json = (var12 = value.split(",")).length;

            for (int txts = 0; txts < json; ++txts) {
                sql = var12[txts];
                sb.append(xx.format(sql)).append(",");
            }

            sb.deleteCharAt(sb.length() - 1);
            sb.append(")");
        }

        System.out.println(sb.toString());
        sql = WidgetManager.addWhere(se, sb.toString());
        List var13 = Db.use(ds).find(sql);
        if (xx.isEmpty(var13)) {
            this.renderJson(value);
        } else {
            JSONObject var14 = new JSONObject();
            var14.put("text_field", se.cn);
            var14.put("data", JsonKit.toJson(var13));
            this.renderJson(var14.toJSONString());
        }
    }

    public void comboJson() {
        String exp = this.getPara("exp");
        User user = User.dao.getByLoginId(getPara("userId"));
        this.setSessionAttr("user",user);
        Object[] os = new Object[]{user};
        MetaField ei = null;
        List list = null;
        String sql;
        try {
            if (xx.isEmpty(exp)) {
                String se = this.getPara(0);
                sql = this.getPara(1);
                ei = MetaField.dao.getByObjectCodeAndEn(se, sql);
                exp = ei.getStr("exp");
            }
            DynamicParse.buildSql(exp, os);
            EovaExp se1 = new EovaExp(exp);
            sql = se1.sql;
            list = (List) CloneUtil.clone(Db.use(se1.ds).findByCache("serviceCache", sql, sql));
        } catch (IOException var8) {
            var8.printStackTrace();
        } catch (ClassNotFoundException var9) {
            var9.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        Record re = new Record();
        if (ei == null || !ei.getBoolean("is_multiple").booleanValue()) {
            re.set("id", "");
            re.set("cn", "");
            list.add(0, re);
        }

        String json = JsonKit.toJson(list);
        this.removeSessionAttr("user");
        this.renderJson(json);
    }
    public static void main(String[] aeg){
        String s="update";
       // s.split("\\/").length;
        System.out.println(s.split("\\/")[ s.split("\\/").length-1]);
    }
    public void findLikeJson()
    {
        String exp = this.getPara("exp");
        String code = this.getPara("code");
        String en = this.getPara("field");
        String like = this.getPara("like");
        User user = User.dao.getByLoginId(getPara("userId"));
        this.setSessionAttr("user", user);
        if (xx.isEmpty(exp))
        {
            MetaField se = MetaField.dao.getByObjectCodeAndEn(code, en);
            exp = se.getStr("exp");
        } else
        {
            try
            {
                exp = URLDecoder.decode(exp, "UTF-8");
            } catch (UnsupportedEncodingException var18)
            {
                var18.printStackTrace();
            }
        }

        exp = DynamicParse.buildSql(exp, new Object[]{this.getSessionAttr("user")});
        EovaExp se1 = new EovaExp(exp);
        String select = se1.simpleSelect;
        String from = se1.from;
        String where = se1.where;
        if (!xx.isEmpty(like))
        {
            where = where + " and " + select.split(",")[1] + " like '%" + like + "%'";
        }
        String ds = se1.ds;
        List eis = se1.getFields();
        int pageNumber = this.getParaToInt("page", Integer.valueOf(1)).intValue();
        int pageSize = this.getParaToInt("rows", Integer.valueOf(10)).intValue();
        ArrayList parmList = new ArrayList();
        where = WidgetManager.getWhere(this, eis, parmList, where);
        Object[] parm = new Object[parmList.size()];
        parmList.toArray(parm);
        String sort = WidgetManager.getSort(this, se1.order);
        String sql = from + where ;
        //List list = Db.use(ds).find(select +" "+ DbUtil.formatSql(sql), parm);
        Page page = Db.use(ds).paginate(pageNumber, pageSize, select, DbUtil.formatSql(sql), parm);
        String json = JsonKit.toJson(page.getList());
        //json = "{\"rows\":" + json + "}";
        this.removeSessionAttr("user");
        this.renderJson(json);
    }

}

